package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.entity.ProductHandover;
import com.blb.entity.User;
import com.blb.entity.Ware;
import com.blb.entity.page.BlbPage;
import com.blb.entity.page.DataTablePage;
import com.blb.service.IProductHandoverService;
import com.blb.service.IWareService;
import com.blb.utils.ResponseHelp;
import com.blb.utils.ShiroUtils;
import com.blb.utils.UUIDUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HuaHao
 * @since 2019-11-18
 */
@Controller
@RequestMapping("handover")
public class ProductHandoverController {

    @Resource
    private IProductHandoverService pHandoverService;

    @Resource
    private IWareService iwareService;

    @RequiresPermissions("handover")
    @GetMapping("page")
    public String page(){
        return "production/dd_rkd";
    }

    @ResponseBody
    @RequestMapping("list")
    public BlbPage home(@RequestParam(required = false, value = "order_id") String order_id,@RequestParam(required = false, value = "handoverDates") String handoverDates, int start, int length) throws ParseException {
        User user = ShiroUtils.getUser();
        SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
        Date date = null;
        if (handoverDates != ""){
            date = formatter.parse(handoverDates);
        }
        List<ProductHandover> list = pHandoverService.getList(user.getCompanyId(),order_id,date,start,length);
        int count = list.size();
        return new DataTablePage(count,count,list);
    }

    @ResponseBody
    @PostMapping("all")
    public BlbPage all(int start, int length, HttpSession session){
        User user = ShiroUtils.getUser();
        //查询分页数据
        IPage<ProductHandover> page = pHandoverService.page(new Page<ProductHandover>(start,length),
                new QueryWrapper<ProductHandover>().eq("state",0).eq("cid",user.getCompanyId()));
        //查询总条数
        int count = pHandoverService.count(new QueryWrapper<ProductHandover>().eq("state",0));
        return new DataTablePage(count, count, page.getRecords());
    }

    @ResponseBody
    @RequestMapping("del")
    public ResponseHelp delete(@RequestBody List<String> ids) {
        pHandoverService.delByIds(ids);
        ResponseHelp responseHelp = new ResponseHelp();
        return  responseHelp;
    }

    @GetMapping("phadd")
    public String adds(Model model){
        User user = ShiroUtils.getUser();
        //根据公司id查询订单中已生产未入库的数据
        String companyId = user.getCompanyId();
        List<ProductHandover> orderId = pHandoverService.findOrderId(companyId);
        model.addAttribute("orderidlist", orderId);

        //根据公司id查询仓库
        List<Ware> allWare = iwareService.findAllWare(companyId);
        model.addAttribute("warelist", allWare);

        return "production/dd_rkd_add";
    }

    @ResponseBody
    @PostMapping("add")
    public ResponseHelp addlist(ProductHandover productHandover) throws ParseException {
        User user = ShiroUtils.getUser();
        productHandover.setId(UUIDUtils.getId());
        productHandover.setState(0);
        productHandover.setType(0);
        productHandover.setCid(user.getCompanyId());
        pHandoverService.save(productHandover);
        ResponseHelp responseHelp = new ResponseHelp();
        return  responseHelp;
    }

    /**
     * 采购列表确定签收将订单添加到交接单
     * @param productHandover
     * @return
     * xuyuanxin
     */
    @ResponseBody
    @PostMapping("addshopping")
    public ResponseHelp addlist1(ProductHandover productHandover){
        User user = ShiroUtils.getUser();
        System.out.println(productHandover);
        productHandover.setId(UUIDUtils.getId());
        productHandover.setCid(user.getCompanyId());
        productHandover.setState(0);
        productHandover.setType(1);
        pHandoverService.save(productHandover);
        ResponseHelp responseHelp = new ResponseHelp();
        return responseHelp;
    }

}

